﻿/*
 * ========================================================================
 * Copyright(c) 2008-2010北京高阳金信信息技术有限公司, All Rights Reserved.
 * ========================================================================
 *  异常处理类
 * 
 * 作者：涂军     时间：2010-1-20
 * 版本：V1.0
 * 
 * 修改者：涂军     时间：2010-2-1
 * 修改说明：加入配置节
 * ========================================================================
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Hisuntech.SystemFramework.Log;

namespace Hisuntech.SystemFramework.Exception
{
    /// <summary>
    /// 异常策略的类别
    /// </summary>
    public enum ExceptionPolicy
    {
        /// <summary>
        /// 写日志
        /// </summary>
        LoggingHandler,
        /// <summary>
        /// 替换
        /// </summary>
        ReplaceHandler,
        /// <summary>
        /// 封装
        /// </summary>
        WrapHandler,
        /// <summary>
        /// 不处理异常
        /// </summary>
        NoneHandler
    }

    public class ExceptionHandler
    {
        /// <summary>
        /// 根据配置文件处理异常
        /// </summary>
        /// <param name="exc">传入的异常</param>
        public static void HandleException(System.Exception exc)
        {
            string exceptionPolicy = ConfigurationSettings.AppSettings["ExceptionPolicy"];
            //配置节为空，不处理异常
            if (string.IsNullOrEmpty(exceptionPolicy))
            {
                throw exc;
            }
            //写日志的异常处理策略
            else if (exceptionPolicy=="LoggingHandler")
            {
                LogClient.AutoWriteLog(exc.ToString());
            }
            //不处理异常
            else if (exceptionPolicy == "NoneHandler")
            {
                throw exc;
            }
        }

        /// <summary>
        /// 根据指定的策略处理异常
        /// </summary>
        /// <param name="exc">传入的异常</param>
        /// <param name="exceptionPolicy">异常策略</param>
        public static void HandleException(System.Exception exc, ExceptionPolicy exceptionPolicy)
        {
            //写日志的异常处理策略
            if (exceptionPolicy.Equals(ExceptionPolicy.LoggingHandler))
            {
                LogClient.AutoWriteLog(exc.ToString());
            }
            //不处理异常
            else if (exceptionPolicy.Equals(ExceptionPolicy.NoneHandler))
            {
                throw exc;
            }
        }
    }
}
